cmake_minimum_required(VERSION 3.22)
project(LevelDB_CLI)

# 设置C++标准
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED on)

# 添加源文件
set (SRC_FILES
    "src/command_line.cpp"
    "src/format_input.cpp"
    "src/parse_input.cpp"
    "src/run_cmd.cpp"
    "src/input_prompt.cpp"
    "src/main.cpp"
)

# 设置头文件目录
set(INCLUDE_DIRS
    "${PROJECT_SOURCE_DIR}/src/"
    "${PROJECT_SOURCE_DIR}/include/"
)

# 如果未设置构建类型，则默认为Debug
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

# 设置编译器标志
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU|Clang")
    if(CMAKE_BUILD_TYPE STREQUAL "Release")
        # 在Release模式下开启O2优化
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
    endif()
    # 开启-Werror
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -pedantic")
endif()

# 生成可执行文件
add_executable(leveldb_cli ${SRC_FILES})
target_include_directories(leveldb_cli PRIVATE ${INCLUDE_DIRS})

# 链接
find_library(LEVELDB_LIBRARY NAMES libleveldb.a PATHS ${CMAKE_SOURCE_DIR}/leveldb/build/)  
if(LEVELDB_LIBRARY)  
    target_link_libraries(leveldb_cli ${LEVELDB_LIBRARY})  
else()  
    message(FATAL_ERROR "Unable to find LevelDB library")  
endif()

# 设置可执行文件的输出目录为bin
set_target_properties(leveldb_cli PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/build")